﻿using ML.IRepository;
using System;
using ML.Entity;
using System.Collections.Generic;
using Dapper;

namespace ML.Repository
{
    /// <summary>
    /// 产品分类仓储实现类
    /// </summary>
    public class ProductCategoryRepository : DBFactory, IProductCategoryRepository
    {
        public Pager<ProductCategoryResponse> List(ProductCategorySearch productCategorySearch)
        {
            //组装调用存储过程参数
            var criteria = new PageCriteria() { TableName = "ML_PRODUCTCATEGORY" };

            criteria.Conditions += string.Format(" AND MerchantId='{0}'", productCategorySearch.MerchantId);
            criteria.Conditions += string.Format(" AND DelMark=0");
            if (!string.IsNullOrEmpty(productCategorySearch.Name_Search))
            {
                criteria.Conditions += string.Format(" AND Name LIKE '%{0}%'", productCategorySearch.Name_Search);
            }

            criteria.PageIndex = productCategorySearch.pageindex;
            criteria.Field = @"*";
            criteria.PageSize = productCategorySearch.pagesize;
            criteria.SortName = productCategorySearch.ordername;
            if (productCategorySearch.orderby)
            {
                criteria.Sort = "DESC";
            }
            else
            {
                criteria.Sort = "ASC";
            }

            //调用存储过程进行查询并且返回
            var list = GetPageData<ProductCategoryResponse>(criteria);
            return list;
        }

        public IEnumerable<ProductCategoryEntity> GetProductCategoryByMerchantId(string id)
        {
            const string sql = @"SELECT * FROM ml_productcategory WHERE MerchantId=@MerchantId AND DelMark=0";
            using (_db)
            {
                return _db.Query<ProductCategoryEntity>(sql, new
                {
                    @MerchantId = id
                });
            }
        }

        public IEnumerable<ProductCategoryEntity> GetNoDelProductCategoryByMerchantId(string id)
        {
            const string sql = @"SELECT * FROM ml_productcategory WHERE MerchantId=@MerchantId AND DelMark=0 AND Enable=0";
            using (_db)
            {
                return _db.Query<ProductCategoryEntity>(sql, new
                {
                    @MerchantId = id
                });
            }
        }

        public IEnumerable<ProductCategoryEntity> GetListByMerchantId(string id)
        {
            const string sql = @"SELECT * FROM ml_productcategory WHERE MerchantId=@MerchantId AND DelMark=0";
            using (_db)
            {
                return _db.Query<ProductCategoryEntity>(sql, new
                {
                    @MerchantId = id
                });
            }
        }

        public ProductCategoryEntity GetById(string id)
        {
            throw new NotImplementedException();
        }

        public bool AddProductCategory(ProductCategoryEntity entity)
        {
            const string sql = @"INSERT INTO ml_productcategory(Id,MerchantId,Name,Sort,Enable,DelMark,CreateTime) VALUES (
                @Id,@MerchantId,@Name,@Sort,@Enable,@DelMark,@CreateTime);";
            using (_db)
            {
                if (_db.Execute(sql, entity) > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool Add(ProductCategoryEntity entity)
        {
            throw new NotImplementedException();
        }

        public bool UpdateProductCategory(ProductCategoryEntity entity)
        {
            const string sql = @"UPDATE ml_productcategory Set Name=@Name,Sort=@Sort,Enable=@Enable,DelMark=@DelMark  WHERE Id=@Id";
            using (_db)
            {
                if (_db.Execute(sql, entity) > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool Update(ProductCategoryEntity entity)
        {
            throw new NotImplementedException();
        }

        public bool Delete(ProductCategoryEntity entity)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            const string sql = @"UPDATE ml_productcategory SET DelMark = @DelMark WHERE Id=@Id";
            using (_db)
            {
                if (_db.Execute(sql, new { @DelMark = (int)DeleteStatus.delete_true, @Id = id }) > 0)
                {
                    return true;
                }
                return false;
            }
        }
    }
}